package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CusApplication;
import com.unionpay.upyzt.model.Media;
import com.unionpay.upyzt.resp.CusApplicationResp;
import com.unionpay.upyzt.resp.CusApplicationSmsCodeResp;
import com.unionpay.upyzt.resp.MediaResp;
import com.unionpay.upyzt.util.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.unionpay.upyzt.example.Main.getResPath;

public class CusApplicationExample {

    /**
     * 运行 Demos
     *
     * @return 个人用户进件返回参数
     */
    public static CusApplicationResp runDemos() throws UpyztException {
        CusApplicationExample cusApplicationExample = new CusApplicationExample();

        // 个人用户进件-短信验证码
        CusApplicationSmsCodeResp cusApplicationSmsCodeResp = cusApplicationExample.smsCode();

        // 个人用户首次进件
        CusApplicationResp cusApplicationResp = cusApplicationExample.create(cusApplicationSmsCodeResp.getMobileNumber());

        // 个人用户进件状态查询（系统订单号）
        cusApplicationResp = cusApplicationExample.retrieveById(cusApplicationResp.getCusApplicationId());

        // 修改个人用户信息
        CusApplicationResp renewCusApplicationResp = cusApplicationExample.renew(cusApplicationResp.getCusId(), cusApplicationSmsCodeResp.getMobileNumber());

        // 个人用户进件状态查询（平台订单号）
        renewCusApplicationResp = cusApplicationExample.retrieveByOutRequestNo(renewCusApplicationResp.getOutRequestNo());

        // 注意：仅首次进件成功会生成电子账簿ID，修改进件不会生成新的电子账簿ID，因此这里返回首次进件的结果，方便后续使用电子账簿ID
        return cusApplicationResp;
    }

    /**
     * 个人用户进件-短信验证码
     *
     * @return 返回
     * @throws UpyztException 异常
     */
    public CusApplicationSmsCodeResp smsCode() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("mobile_number", "1" + StringUtils.randomNumeric(10));
        System.out.println("------- 个人用户进件-短信验证码 -------");
        CusApplicationSmsCodeResp resp = CusApplication.sendSmsCode(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 个人用户首次进件
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CusApplicationResp create(String mobileNumber) throws UpyztException {
        File file = new File(getResPath("id_card_sample.jpg"));
        MediaResp mediaResp = Media.uploadImage(file);

        Map<String, Object> idCard = new HashMap<>();
        idCard.put("copy", mediaResp.getMediaId());
        idCard.put("national", mediaResp.getMediaId());
        idCard.put("number", Upyzt.encryptField("3101002000011" + StringUtils.randomNumeric(5)));
        idCard.put("name", Upyzt.encryptField("张三"));
        idCard.put("valid_time", "2001-01-01,2050-01-01");

        Map<String, Object> settleAcct = new HashMap<>();
        settleAcct.put("bank_code", "104100000004");
        settleAcct.put("name", Upyzt.encryptField("张三"));
        settleAcct.put("bank_address_code", "110105");
        settleAcct.put("bank_branch_code", "001588044013");
        settleAcct.put("bank_acct_no", Upyzt.encryptField("123456789"));

        Map<String, Object> params = new HashMap<>();
        params.put("out_request_no", Main.generateRandomOutNo());
        params.put("id_card", idCard);
        params.put("mobile_number", Upyzt.encryptField(mobileNumber));
        params.put("settle_acct", settleAcct);
        // 是否必传取决于平台业务参数配置
        params.put("sms_code", "999999");
        params.put("password", Upyzt.encryptField("pa55w0rd"));

        System.out.println("------- 个人用户首次进件 -------");
        CusApplicationResp resp = CusApplication.create(params);
        System.out.println(resp);

        return resp;
    }

    /**
     * 个人用户进件状态查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CusApplicationResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 个人用户进件状态查询（系统订单号） -------");
        CusApplicationResp resp = CusApplication.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 修改个人用户信息
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CusApplicationResp renew(String cusId, String mobileNumber) throws UpyztException {
        File file = new File(getResPath("id_card_sample.jpg"));
        MediaResp mediaResp = Media.uploadImage(file);

        Map<String, Object> idCard = new HashMap<>();
        idCard.put("copy", mediaResp.getMediaId());
        idCard.put("national", mediaResp.getMediaId());
        idCard.put("name", Upyzt.encryptField("李四"));
        idCard.put("valid_time", "2001-01-02,2050-01-02");

        Map<String, Object> settleAcct = new HashMap<>();
        settleAcct.put("bank_code", "104100000004");
        settleAcct.put("name", Upyzt.encryptField("李四"));
        settleAcct.put("bank_address_code", "110105");
        settleAcct.put("bank_branch_code", "001588044013");
        settleAcct.put("bank_acct_no", Upyzt.encryptField("123456789"));

        Map<String, Object> params = new HashMap<>();
        params.put("cus_id", cusId);
        params.put("out_request_no", Main.generateRandomOutNo());
        params.put("id_card", idCard);
        params.put("mobile_number", Upyzt.encryptField(mobileNumber));
        params.put("settle_acct", settleAcct);
        params.put("sms_code", "999999");

        System.out.println("------- 修改个人用户信息 -------");
        CusApplicationResp resp = CusApplication.renew(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 个人用户进件状态查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CusApplicationResp retrieveByOutRequestNo(String outRequestNo) throws UpyztException {
        System.out.println("------- 个人用户进件状态查询（平台订单号） -------");
        CusApplicationResp resp = CusApplication.retrieveByOutRequestNo(outRequestNo);
        System.out.println(resp);
        return resp;
    }

}
